package cn.xawl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.xawl.gulimall.product.entity.ProductAttrValueEntity;
import cn.xawl.gulimall.product.service.ProductAttrValueService;
import cn.xawl.gulimall.product.vo.AttrResVo;
import cn.xawl.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.xawl.gulimall.product.entity.AttrEntity;
import cn.xawl.gulimall.product.service.AttrService;
import cn.xawl.common.utils.PageUtils;
import cn.xawl.common.utils.R;



/**
 * 商品属性
 *
 * @author lh
 * @email lhm49869999@163.com
 * @date 2020-06-17 14:15:22
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 获取分类规格参数（根据不同的类型）
     * /product/attr/base/list/{catalogId}
     * /product/attr/sale/list/{catalogId}
     */
    @RequestMapping("/{type}/list/{catalogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catalogId") Long catalogId, @PathVariable("type") String type){
//        PageUtils page = attrService.queryPage(params);
        PageUtils page = attrService.getAttrList(params, catalogId, type);
        return R.ok().put("page", page);
    }

    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);

        return R.ok().put("data",entities);
    }

    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities){

        productAttrValueService.updateSpuAttr(spuId,entities);

        return R.ok();
    }



    /**
     * 查询属性详情（用来在修改时显示最新数据）
     * /product/attr/info/{attrId}
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrResVo attr = attrService.getAttrDetail(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存属性【规格参数，销售属性】
     * /product/attr/saveSkuFullReduction
     */
    @RequestMapping("/save")
        public R save(@RequestBody AttrVo attr){
//		attrService.saveSkuFullReduction(attr);
        attrService.saveAttr(attr);
        return R.ok();
    }



    /**
     * 修改，由于修改时还要修改所属分组，所以要修改关联表中的数据
     */
    @RequestMapping("/update")
        public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttrRelation(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
